package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.CfgAreaDao;
import com.baitengsoft.patentapimonitor.api.entity.CfgAreaEntity;
import com.baitengsoft.patentapimonitor.api.service.CfgAreaService;


import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2017/12/19.
 */
@Service
public class CfgAreaServiceImpl extends BaseServiceImpl<CfgAreaEntity> implements CfgAreaService {
    @Autowired
    private CfgAreaDao cfgAreaDaoDao;
    @Override
    public List<String> getProvince() {
        try {
//            Mapper<CfgAreaEntity> mapper= this.getMapper();
//            Example example=new Example(CfgAreaEntity.class);
//            example.setDistinct(true);
//            List<CfgAreaEntity> entities=mapper.selectByExample(example);
//            List<CfgAreaEntity> entities=queryAll();
//            List<String> provinces=new ArrayList<>();
//            String str="";
//            entities.forEach(x->{
//
//                provinces.add(x.getProvince());
//            });
            List<CfgAreaEntity> entities= cfgAreaDaoDao.searchProvince();
            List<String> provinces=new ArrayList<>();
            entities.forEach(x->{
                provinces.add(x.getProvince());
            });
            return provinces;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<String> getCity(String province) {
        try {
            List<CfgAreaEntity> entities= cfgAreaDaoDao.searchCity( province);
            List<String> citys=new ArrayList<>();
            entities.forEach(x->{
                citys.add(x.getCity());
            });
            return citys;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<String> getPlace(String city) {
        try {
            List<CfgAreaEntity> entities= cfgAreaDaoDao.searchPlace(city);
            List<String> citys=new ArrayList<>();
            String jsonPlace=entities.get(0).getPlace();
            JsonParser parser=new JsonParser();
            JsonArray jsonElement=parser.parse(jsonPlace).getAsJsonArray();
            for(JsonElement element:jsonElement){
                citys.add(element.getAsString());
            }
            return citys;
        } catch (Exception e) {
            return null;
        }
    }
}
